package com.itruth.base;

import java.util.concurrent.ConcurrentHashMap;

import com.itruth.data.entity.ResponseData;

/**
 * 存放所有服务器的响应通知
 * 1,客户端注册与之HTTP请求相关的通知服务（即相关的ACTION标志）,并注册回调监听listener
 * 2,线程处理的HTTP请求响应回来后，通过ACTION标志，找到对应的listener回调，执行回调方法
 * 3,客户端收到通知，执行后续操作
 * @author yj
 *
 */
public class LocalNotice {

	/**
	 * 注册相关响应
	 */
	public static final int ACTION_REGISTER = 1000;
	
	/**
	 * 查询分享数据
	 */
	public static final int ACTION_QUERY_SHAREDATA = 1001;
	
	private static LocalNotice instance;
	
	private ConcurrentHashMap<Integer, NoticeCallback> localNotices;
	
	private LocalNotice () {
		localNotices = new ConcurrentHashMap<Integer, NoticeCallback>();
	}
	
	public static LocalNotice getIns() {
		
		if (null == instance) {
			synchronized (LocalNotice.class) {
				if (null == instance) {
					instance = new LocalNotice();
				}
			}
		}
		
		return instance;
		
	}
	
	/**
	 * 
	 * @param actionTag 响应标志
	 * @param data 响应返回的数据
	 * @param status 响应状态
	 * @param protocolTag 通信协议代码
	 */
	public void sendNotice(Integer actionTag, String jsonData, int status, String protocolTag) {
		
		NoticeCallback callback = localNotices.get(actionTag);
		
		synchronized (LocalNotice.class) {
			if (null != callback) {
				ResponseData resData = new ResponseData();
				resData.setStatus(status);
				resData.setData(jsonData);
				resData.setProtocolTag(protocolTag);
				callback.callback(resData);
			}
		}
		
	}
	
	public void regNotice(Integer actionTag, NoticeCallback callback) {
		
		localNotices.put(actionTag, callback);
		
	}
	
	public void cancelNotice(Integer actionTag) {
		localNotices.remove(actionTag);
	}

	
	
	
}














